// component/inputCount/inputCount.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    initNum:{
      type: Number,
      value: 0,
    },
    maxNum:{
      type: Number,
      value: 10000,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
     count:0
  },

  lifetimes:{
    attached: function () {
      // 在组件实例进入页面节点树时执行
      let num = this.data.initNum;
      this.setData({
        count:num
      })
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    
    //直接编写数量
    _changeCount:function(e){
      let c = e.detail.value;
      if(c===null){
        wx.showToast({
          title: '请输入数量',
        })
        c = 0;
      } else if (c < 0){
        wx.showToast({
          title: '不能小于0',
        })
        c = 0;
      } else if (c > this.data.maxNum){
        wx.showToast({
          title: '不能大于'+this.data.maxNum,
          image:'/images/gantan.png'
        })
        c = this.data.maxNum;
      }
      c = parseInt(c);

      this.setData({
         count: c
      })
      
      var myEventDetail = { "value": c } // detail对象，提供给事件监听函数
      var myEventOption = {} // 触发事件的选项
      this.triggerEvent('myevent', myEventDetail, myEventOption);   
    },
    //数量加
    _plus:function(){
      if (this.data.count >= this.data.maxNum){
        return
      }

      let c = this.data.count + 1;
      this.setData({
        count:c
      });
      var myEventDetail = {"value":c} // detail对象，提供给事件监听函数
      var myEventOption = {} // 触发事件的选项
      this.triggerEvent('myevent', myEventDetail, myEventOption);
    },
    //数量减
    _reduce:function(){
      let c = this.data.count - 1;
      if(c >= 0) {
        this.setData({
          count: c
        });
      }else {
        c = 0;
      }
      var myEventDetail = { "value": c } // detail对象，提供给事件监听函数
      var myEventOption = {} // 触发事件的选项
      this.triggerEvent('myevent', myEventDetail, myEventOption);      
    }
  }
})
